﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DCTAccess.Business;
using DCTLib.Common;
using DCTAccess.Business.Percent;
using DCTAccess.Business.LevelConfig;
using Obout.Grid;

public partial class AdminZone_PercentConfig : System.Web.UI.Page
{
    #region Properties
    private string Message
    {
        get { return lblError.Text; }
        set { lblError.Text = value; }
    }
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable dtPercent = PercentController.GetConfig();
            DataTable dtLevel = LevelController.GetConfig();
            Grid1.DataSource = dtPercent;
            Grid1.DataBind();
            Grid2.DataSource = dtLevel;
            Grid2.DataBind();
            BindDataToDropBox();
        }
    }

    protected void BindDataToDropBox()
    {
        for (int i = 1; i <= 20; i++)
        {
            ddlLevel.Items.Add(i.ToString());
        }
    }

    protected void btnReset_Click(object sender, EventArgs e)
    {
        ClearInputValues();
    }

    protected void DeleteRecord(object sender, GridRecordEventArgs e)
    {
        Percent percet = new Percent();
        percet.ConfigID = Convert.ToInt32(e.Record["ConfigID"].ToString());
        PercentController.Delete(percet);
        //RedirectPage();
    }

    protected void DeleteRecord2(object sender, GridRecordEventArgs e)
    {
        Level lvt = new Level();
        lvt.Id = Convert.ToInt32(e.Record["ID"].ToString());
        LevelController.Delete(lvt);
        //RedirectPage();
    }

    protected void UpdateRecord(object sender, GridRecordEventArgs e)
    {
        Level lvt = new Level();
        lvt.Id = Convert.ToInt32(e.Record["ConfigID"].ToString());
        LevelController.Delete(lvt);
    }

    private void RedirectPage()
    {
        Response.Redirect(AppUrls.AdminZone.CONFIG);
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (ValidateData())
        {
            Percent percet = GetPercentConfig();
            if (PercentController.Insert(percet))
            {
                Message = AppMessages.CREATE_SUCCESSFULLY;
                Response.Redirect(AppUrls.AdminZone.CONFIG);
            }
            else
            {
                Message = AppMessages.CANNOT_CREATE;
            }
        }
        else
        {
            Message = "Các trường dữ liệu nhập vào không hợp lệ";
        }
    }

    private void ClearInputValues()
    {
        txtNumOfPeople.Text = string.Empty;
        txtPercent.Text = string.Empty;
    }

    private bool ValidateData()
    {
        bool validated = true;

        if (!ValidationHelper.IsNumber(txtNumOfPeople.Text))
        {
            Message += AppMessages.RequireField("NumOfPeople") + "<br/>";
            validated = false;
        }
        else if (!ValidationHelper.IsNumber(txtPercent.Text))
        {
            Message += AppMessages.RequireField("Percent") + "<br/>";
            validated = false;
        }
        return validated;
    }

    private bool ValidateDataLevelconfig()
    {
        bool validated = true;

        if (!ValidationHelper.IsNumber(txtNumofPeopleLv.Text))
        {
            lblLevelError.Text += AppMessages.RequireField("NumOfPeople") + "<br/>";
            validated = false;
        }
        else if (!ValidationHelper.IsNumber(txtPercentLv.Text))
        {
            lblLevelError.Text += AppMessages.RequireField("Percent") + "<br/>";
            validated = false;
        }
        return validated;
    }


    private Percent GetPercentConfig()
    {
        Percent pecet = new Percent();
        pecet.NumOfPeople = int.Parse(txtNumOfPeople.Text);
        pecet.PercentConfig = int.Parse(txtPercent.Text);
        return pecet;
    }

    private Level GetLevelconfig()
    {
        Level lvt = new Level();
        lvt.Level1 = Convert.ToInt32(ddlLevel.SelectedValue.ToString());
        lvt.NumOfPeople = Convert.ToInt32(txtNumofPeopleLv.Text);
        lvt.Percent = Convert.ToDecimal(txtPercentLv.Text);
        return lvt;
    }

    protected void btnInsert_Click(object sender, EventArgs e)
    {
        if (ValidateDataLevelconfig())
        {
            Level lvt = GetLevelconfig();
            lblLevelError.Text = lvt.Level1.ToString();
            lblLevelError.Text += "<br/>: " + lvt.NumOfPeople;
            lblLevelError.Text += "<br/>: " + lvt.Percent;

            Level lvConfig = LevelController.GetConfigByID(lvt.Level1);
            if (lvConfig != null)
            {
                lblLevelError.Text = "Cấu hình level này đã tồn tại";
            }
            else
            {
                if (LevelController.Insert(lvt))
                {
                    lblLevelError.Text = AppMessages.CREATE_SUCCESSFULLY;
                    Response.Redirect(AppUrls.AdminZone.CONFIG);
                }
                else
                {
                    lblLevelError.Text = AppMessages.CANNOT_CREATE;
                }
            }
        }
        else
        {
            lblLevelError.Text = "* Các trường dữ liệu nhập vào không hợp lệ";
        }
    }

    protected void btnClearBox_Click(object sender, EventArgs e)
    {
        txtNumofPeopleLv.Text = "";
        txtPercentLv.Text = "";
    }

    protected void ddlLevel_SelectedIndexChanged(object sender, EventArgs e)
    {
        txtNumofPeopleLv.Enabled = false;
        txtPercentLv.Enabled = false;
    }
}
